(function($){

    $.fn.requestForm = function(){
        var $this = this;

        var requestForm = {

            init : function(){

                this.category = $($this).attr('href');
                this.cat = $(this.category);
                this.services_form = this.cat.find('.services_form_wrapper');
                this.paginator = this.cat.find('.services_form_paginator');
                this.fieldsets = this.services_form.find('fieldset');
                this.next_btn = this.services_form.find('.services_next');
                this.back_btn = this.services_form.find('.services_back');

                /*show first fieldset*/
                this.fieldsets.hide();
                this.fieldsets.eq(0).show();

                /*add paginator spans*/
                var dots_quantity = this.paginator.find('span').size();

                if(dots_quantity === 0){
                    this.fieldsets.each(function() {
                        requestForm.paginator.append('<span></span>');
                    });
                }

                //reset none display class
                this.next_btn.removeClass('not_active_btn');

                //error spans
                var error_fieldset = $('.form_errors').parents('fieldset'),
                    error_block = error_fieldset.parent().parent().parent(),
                    error_spans = error_block.find('.services_form_paginator span');

                error_fieldset.each(function(){
                    var erIndex = $(this).index();

                    error_spans.eq(erIndex - 1).addClass('error_span');
                });

                /*add active class to first span*/
                this.paginator.find('span.active_span').removeClass('active_span');
                this.paginator.find('span').eq(0).addClass('active_span');

                /*event handler on paginator spans*/
                this.paginator.find('span').unbind('click').bind('click',this.pagination);

                /*event handler on next button*/
                this.next_btn.unbind('click').bind('click',this.nextBtn);


                /*event handler on back button*/
                this.back_btn.unbind('click').bind('click',this.backBtn);
            },
            backBtn : function(){
                var old_active = requestForm.paginator.find('span.active_span'),
                    old_active_index = old_active.index(),
                    new_active = old_active_index - 1,
                    self = $(this);

                //reset none display class
                requestForm.next_btn.removeClass('not_active_btn');

                $('span.not_active_btn').removeClass('not_active_btn');

                if(!old_active.is('span:first-child')){
                    old_active.removeClass('active_span');
                    requestForm.fieldsets.hide();

                    requestForm.paginator.find('span').eq(new_active).addClass('active_span');
                    requestForm.fieldsets.eq(new_active).show();
                }
            },
            nextBtn : function(){
                var old_active = requestForm.paginator.find('span.active_span'),
                    old_active_index = old_active.index(),
                    new_active = old_active_index + 1,
                    self = $(this);

                //check if this is last span
                if(requestForm.paginator.find('span').eq(new_active).is('span:last-child')){
                    self.addClass('not_active_btn');
                }


                if(!old_active.is('span:last-child')){
                    old_active.removeClass('active_span');
                    requestForm.fieldsets.hide();

                    requestForm.paginator.find('span').eq(new_active).addClass('active_span');
                    requestForm.fieldsets.eq(new_active).show();
                }

            },
            pagination : function(){
                var self = $(this),
                    active = self.index();
                requestForm.fieldsets.hide();

                requestForm.fieldsets.eq(active).show();

                requestForm.paginator.find('span.active_span').removeClass('active_span');
                requestForm.paginator.find('span').eq(active).addClass('active_span');

                //hide or show next button
                if(self.is(':last-child')){
                    self.parent().parent().find('.services_next').addClass('not_active_btn');
                }
                else{
                    self.parent().parent().find('.services_next').removeClass('not_active_btn');
                }
            }

        };
        requestForm.init();
    }

})(jQuery)

